import json
import os
import pprint

import requests
import execjs
from datetime import datetime

cookies = {
    'privacyPolicyPopup': 'false',
    '__lg_stoken__': 'f74fa76fd756c33c66a949d7483807d0a529c168647bc3258c1b1ec53b4a307d6009757fed68666df1247e2e26230f4f4d0f1702a4febcf35babb0f411509392f4653de95524',
    'JSESSIONID': 'ABAABJAABDBABEFF5EBE79858E5268224A5EE09426B0A0F',
    'WEBTJ-ID': '20240810150353-1913b19d0e3c82-06501729ce221d-26001e51-2073600-1913b19d0e4bc1',
    'user_trace_token': '20240810150354-d133c00d-f12a-493c-99c1-34f791c20b3e',
    'LGUID': '20240810150354-7fa96d4d-aa2d-4363-8d1c-43d09d4cc952',
    'sensorsdata2015session': '%7B%7D',
    'TG-TRACK-CODE': 'index_code',
    'SEARCH_ID': '0d3d407f9b604cc287f855ceaca673e2',
    'X_MIDDLE_TOKEN': 'ba3aa272719b1660a72129343136e9fb',
    'gate_login_token': 'v1####c6c98cca27d02af50b34149c15bb8343f1e2b26de5e706b24867efe4a708d3ca',
    '_putrc': '8B161A51F69B3D31123F89F2B170EADC',
    'login': 'true',
    'unick': '%E7%94%A8%E6%88%B70545',
    'hasDeliver': '0',
    'privacyPolicyPopup': 'false',
    'X_HTTP_TOKEN': '94f5006dafacab3c6204923271b053423ed2c404e9',
    'LGRID': '20240811010147-b0650a17-ed0a-4adc-b9bb-0f4bed3c3a58',
    '__RESUME_COMPLETE_POPOVER__': '1',
    'sensorsdata2015jssdkcross': '%7B%22distinct_id%22%3A%221913b19d8b36ab-0f11ae8cb2486-26001e51-2073600-1913b19d8b4633%22%2C%22first_id%22%3A%22%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E4%BB%98%E8%B4%B9%E5%B9%BF%E5%91%8A%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24latest_utm_source%22%3A%22pc_search_right_jljx_1%22%2C%22%24os%22%3A%22Windows%22%2C%22%24browser%22%3A%22Chrome%22%2C%22%24browser_version%22%3A%22127.0.0.0%22%7D%2C%22%24device_id%22%3A%221913b19d8b36ab-0f11ae8cb2486-26001e51-2073600-1913b19d8b4633%22%7D',
}


def saveJson(dataDict):
    with open("lagou.json", "w", encoding="utf-8") as f:
        f.write(json.dumps(dataDict, ensure_ascii=False))


def readJsonData():
    with open("lagou.json", "r", encoding="utf-8") as f:
        data = json.load(f)
        XK = data.get('XK')
        XSS = data.get('XSS')
        expireTime = data.get('expireTime', 0)
    return XK, XSS, expireTime


def getSecret():
    if not os.path.exists("lagou.json"):
        # 创建这个文件
        saveJson({})
    XK, XSS, expireTime = readJsonData()
    # 解析 expireTime 字符串为 datetime 对象
    if expireTime == 0 or datetime.now().timestamp() - datetime.strptime(expireTime, "%Y-%m-%d %H:%M:%S").timestamp() >= 0:
        url = "https://gate.lagou.com/system/agreement"
        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36",
            "Referer": "https://www.lagou.com/",
            "Host": "gate.lagou.com",
            "Connection": "keep-alive",
            "Accept": "application/json",
            "Content-type": "application/json",
        }
        rsaEncryptData = "mz+eO99ACMV0Z1+IE9C7eKXy1wCK4LTp9bJNuojEK0hhMO6wqDE8z9poxLBmqvmK51fDlINUIBLnFMqrtr2fSOw4zhw9R9NSIDSEdYw/nYYhmwiUzcBR1YPu8LIqnRKVPY0W8oA5N0HRrVliidEh+OtvbCpGYsmGJ0cXoG7LuLhqEt41bu5/PfHax2QWd2GVqauosjRu/+8iknVW92ei8UC0p8vp3GllTdU74sDiVD+LW+XaDzeMHdOTlpaC8LAiXClD6bPJQ/NMO1TpojNwe/WBrWpmaeJ6dZdida04DVOWyfQzrRa8D0fW9Ad1TlD+0vNO+4YwjwcftgOC8VjTcg=="
        data = {
            "secretKeyDecode": rsaEncryptData
        }
        res = requests.post(url, headers=headers, json=data)
        XK = res.json()["content"]["secretKeyValue"]
        XSS = json.dumps({"secret": XK}, ensure_ascii=False)
        expireTime = res.json()["content"]["expireTime"]
        dataDict = {
            "XK": XK,
            "XSS": XSS,
            "expireTime": expireTime,
        }
        saveJson(dataDict)
    return XK, XSS


def getResponse(ct, kd, pn, XK, XSS):
    headers = {
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Cache-Control': 'no-cache',
        'Connection': 'keep-alive',
        'Origin': 'https://www.lagou.com',
        'Pragma': 'no-cache',
        'Referer': 'https://www.lagou.com/wn/',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'same-origin',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36',
        'accept': 'application/json, text/plain, */*',
        'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'sec-ch-ua': '"Not)A;Brand";v="99", "Google Chrome";v="127", "Chromium";v="127"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
        'traceparent': '00-c351cb0307bf6d56e950c500ba46fdcb-0834948a5c4927b7-01',
        'x-anit-forge-code': 'c9fc25fe-3b24-400d-ba10-581a553ad3d6',
        'x-anit-forge-token': 'eaca12f3-5e73-4890-ad3e-613b30a1a84d',
    }
    # kd为keywords 搜索词   pn为 pagenumber 页数
    body_dict = {
        "first": "true",
        "needAddtionalResult": "false",
        "city": ct,
        "fromSearch": "true",
        "kd": kd,
        "pn": str(pn)
    }
    # 使用 json.dumps() 将字典转换为 JSON 字符串;设置 ensure_ascii=False 来确保中文字符不会被转义。
    body = json.dumps(body_dict, ensure_ascii=False)

    with open('./spider.js', 'r', encoding='utf-8') as f:
        text = f.read()
        Xs = execjs.compile(text).call('getXS', body)
        EncryptBody = execjs.compile(text).call('It', body)  # 加密请求体
    d = {
        'data': EncryptBody   # 请求体可以使用body也就是不加密也能请求成功,前端页面显示为加密值是为了反爬, 需要断点找出这个接口的载荷即原始的请求体的格式，在这里是body_dict
    }
    headers['X-S-HEADER'] = Xs   # 虽然页面上显示这个三个值都是动态的但是这个X-S-HEADER参数不带也能请求成功
    print('头部X-S-HEADER:', headers['X-S-HEADER'])
    headers['X-K-HEADER'] = f'{XK}'  # 不携带这个请求响应的是乱码文本  参数过期时间为1天
    print('头部X-K-HEADER:', headers['X-K-HEADER'])
    headers['X-SS-REQ-HEADER'] = f'{XSS}'  # 不携带这个请求响应为空导致后续程序json序列化报错  参数过期时间为1天
    print('头部X-SS-REQ-HEADER:', headers['X-SS-REQ-HEADER'])
    print('请求体加密body:', d['data'])
    response = requests.post('https://www.lagou.com/jobs/v2/positionAjax.json', cookies=cookies, headers=headers, data=d)
    print("请求响应:", response.text)
    data = execjs.compile(open('./spider.js', 'r', encoding='utf-8').read()).call('getData', response.json()['data'])
    print("解密后的响应:", data)
    pprint.pprint(data)


if __name__ == '__main__':
    city = '全国'
    keywords = '爬虫工程师'
    pagenumber = 8
    XK, XSS = getSecret()
    getResponse(city, keywords, pagenumber, XK, XSS)
